<?php
/**
 * Created by PhpStorm.
 * User: liuxiaojie
 * Date: 2019-03-13
 * Time: 15:03
 */

namespace app\tools;

class RedPacket
{

    public $mch_id;
    public $wxappid;
    public $send_name;
    public $wishing;
    public $key;

    function __construct()
    {
        $this->mch_id = config('mch_id');
        $this->wxappid = config('wx.appid');
        $this->send_name = '安吉虎猫';
        $this->wishing = '恭喜发财!!!';
        $this->key = config('api_key');
    }

    function ordinary($param){

        $url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";

        $nonce_str = Helper::smsCode(32, 5);

        $post = [

            "nonce_str"=> $nonce_str,
            "mch_billno"=> $param['mch_billno'],
            "mch_id"=> $this->mch_id,
            "wxappid"=> $this->wxappid,
            "send_name"=> $this->send_name,
            "re_openid"=> $param['re_openid'],
            "total_amount"=> $param['total_amount'],
            "total_num"=> $param['total_num'],
            "wishing"=> $this->wishing,
            "client_ip"=> getIp(),
            "act_name"=> $param['act_name'],
            "remark"=> $param['remark'],
            "scene_id"=> $param['scene_id'],

        ];

        $sign = $this->sign($post);
        $post['sign'] = $sign;

//        exit(array2xml($post));
        $path = ROOT_PATH."sslpath/";

        $result =  curl($url, array2xml($post), [], [
            'cert'=> $path.config("ssl_path.cert"),
            'key'=> $path.config("ssl_path.key"),
        ]);

        $result = (array)simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA);

//        dump($result);

        if($result['return_code'] == "SUCCESS"){
            return true;
        }else{
            return $result['return_msg'];
        }
    }

    function sign($param){

        ksort($param);

        $str = "";

        foreach ($param as $k=> $v){
            if($v){
                $str .= $k."=".$v."&";
            }
        }

        $stringSignTemp = $str."key=".$this->key;
        $sign_value = strtoupper(md5($stringSignTemp));

        return $sign_value;

    }

    function searchOrder($param){

        $url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo";

        $nonce_str = Helper::smsCode(32, 5);

        $post = [

            "nonce_str"=> $nonce_str,
            "mch_billno"=> $param['mch_billno'],
            "mch_id"=> $this->mch_id,
            "appid"=> $this->wxappid,
            "bill_type"=> 'MCHT',

        ];

        $sign = $this->sign($post);
        $post['sign'] = $sign;

        $path = ROOT_PATH."sslpath/";

        $result =  curl($url, array2xml($post), [], [
            'cert'=> $path.config("ssl_path.cert"),
            'key'=> $path.config("ssl_path.key"),
        ]);

        $result = (array)simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA);

//        exit(dump($result));
//        dump($result);

        if($result['result_code'] == "SUCCESS" && $result['status'] == "RECEIVED"){

            return [
                "status"=> 0,
                "detail_id"=> $result['detail_id']
            ];

        }elseif($result['result_code'] == "SUCCESS" && $result['status'] == "FAILED") {

            Log::writeLog($result['reason']);

            return [
                "status"=> 1,
            ];

        }else{

            return [
                "status"=> 2,
            ];

        }

    }

}